//
//  SmartCounselRecordView.m
//  Zinipad
//
//  Created by ZeLkOvA on 12. 6. 7..
//  Copyright (c) 2012년 __MyCompanyName__. All rights reserved.
//

#import "SmartCounselRecordView.h"

@interface SmartCounselRecordView ()

@end

@implementation SmartCounselRecordView

@synthesize leftMenuScrollView;
@synthesize wallPaperView;
@synthesize flooringView;
@synthesize NameTextField;
@synthesize PhoneTextField;
@synthesize EmailTextField;
@synthesize MemoTextField;

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:self.view.window];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    
    //    [super viewWillAppear:YES];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    _currentTextField = textField;
}

-(BOOL)textFieldShouldReturn:(UITextField*)textFieldView
{
    [textFieldView resignFirstResponder];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField*)textFieldView
{
    _currentTextField = nil;
}

-(void)keyboardDidShow:(NSNotification *)notification
{
    if (_isKeyboardShow)
    {
        return;
    }
    
        NSDictionary* info = [notification userInfo];
        
        NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect = [self.view convertRect:[aValue CGRectValue] fromView:nil];
        
        CGRect viewFrame = [leftMenuScrollView frame];
        viewFrame.size.height = viewFrame.size.height - keyboardRect.size.height;
        leftMenuScrollView.frame = viewFrame;
        
        CGRect textFieldRect = [_currentTextField frame];
        [leftMenuScrollView scrollRectToVisible:CGRectMake(textFieldRect.origin.x, textFieldRect.origin.y+300, textFieldRect.size.width, textFieldRect.size.height)  animated:YES];
        
        _isKeyboardShow = YES;
    
}

-(void)keyboardDidHide:(NSNotification *)notification
{
    
        NSDictionary* info = [notification userInfo];
        
        NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect = [self.view convertRect:[aValue CGRectValue] fromView:nil];
        CGRect viewFrame = [leftMenuScrollView frame];
        viewFrame.size.height += keyboardRect.size.height;
//        NSLog(@"%f key",keyboardRect.size.height);
        leftMenuScrollView.frame = viewFrame;
        [leftMenuScrollView scrollRectToVisible:viewFrame animated:YES];
        _isKeyboardShow = NO;
    
}

-(void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
  
    nPriceValue = -1;
    nItemType = 0;
    
    // 데이터 베이스
	// Do any additional setup after loading the view.
    UIButton* BackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [BackButton setFrame:CGRectMake(20, 20, 70, 40)];
    [BackButton setTitle:[NSString stringWithFormat:@"이전"] forState:UIControlStateNormal];
    [BackButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [BackButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [BackButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [BackButton addTarget:self action:@selector(BackButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:BackButton];
    [self leftSelectView];
}
-(void)BackButtonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)leftSelectView
{
    leftMenuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 70, 400, 748-90)];
    [leftMenuScrollView setBackgroundColor:[UIColor blackColor]];
    [leftMenuScrollView setContentSize:CGSizeMake(400, 748)];
    [self.view addSubview:leftMenuScrollView];
    
    UIButton* PriceLowButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [PriceLowButton setFrame:CGRectMake(40, 40, 70, 40)];
    [PriceLowButton setTitle:[NSString stringWithFormat:@"알뜰"] forState:UIControlStateNormal];
    [PriceLowButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [PriceLowButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [PriceLowButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [PriceLowButton addTarget:self action:@selector(PriceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [PriceLowButton setTag:0];
    [leftMenuScrollView addSubview:PriceLowButton];
    
    UIButton* PriceMidButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [PriceMidButton setFrame:CGRectMake(140, 40, 70, 40)];
    [PriceMidButton setTitle:[NSString stringWithFormat:@"합리"] forState:UIControlStateNormal];
    [PriceMidButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [PriceMidButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [PriceMidButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [PriceMidButton addTarget:self action:@selector(PriceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [PriceMidButton setTag:1];
    [leftMenuScrollView addSubview:PriceMidButton];
    
    UIButton* PriceTopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [PriceTopButton setFrame:CGRectMake(240, 40, 70, 40)];
    [PriceTopButton setTitle:[NSString stringWithFormat:@"친환경"] forState:UIControlStateNormal];
    [PriceTopButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [PriceTopButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [PriceTopButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [PriceTopButton addTarget:self action:@selector(PriceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [PriceTopButton setTag:2];
    [leftMenuScrollView addSubview:PriceTopButton];
    
    UIButton* WallpaperButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [WallpaperButton setFrame:CGRectMake(40, 140, 120, 60)];
    [WallpaperButton setTitle:[NSString stringWithFormat:@"벽지"] forState:UIControlStateNormal];
    [WallpaperButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [WallpaperButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [WallpaperButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [WallpaperButton addTarget:self action:@selector(WallpaperOrFlooringButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [WallpaperButton setTag:10];

    [leftMenuScrollView addSubview:WallpaperButton];
    
    UIButton* FlooringButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [FlooringButton setFrame:CGRectMake(180, 140, 120, 60)];
    [FlooringButton setTitle:[NSString stringWithFormat:@"바닥재"] forState:UIControlStateNormal];
    [FlooringButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [FlooringButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [FlooringButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [FlooringButton addTarget:self action:@selector(WallpaperOrFlooringButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [FlooringButton setTag:11];
    [leftMenuScrollView addSubview:FlooringButton];
    
    if(nItemType == 0)
        [self wallpaperViewSelect];
    else 
    {
    }

}
-(void)wallpaperViewSelect
{
    self.wallPaperView = [[UIView alloc] initWithFrame:CGRectMake(10, 240, 380, 500)];
    
    UIButton* WallpaperBaseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [WallpaperBaseButton setFrame:CGRectMake(20, 40, 60, 50)];
    [WallpaperBaseButton setTitle:[NSString stringWithFormat:@"베이스"] forState:UIControlStateNormal];
    [WallpaperBaseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [WallpaperBaseButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [WallpaperBaseButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [WallpaperBaseButton addTarget:self action:@selector(WallpaperUseButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [WallpaperBaseButton setTag:20];
    [wallPaperView addSubview:WallpaperBaseButton];
    
    UIButton* WallpaperPointButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [WallpaperPointButton setFrame:CGRectMake(90, 40, 60, 50)];
    [WallpaperPointButton setTitle:[NSString stringWithFormat:@"포인트"] forState:UIControlStateNormal];
    [WallpaperPointButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [WallpaperPointButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [WallpaperPointButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [WallpaperPointButton addTarget:self action:@selector(WallpaperUseButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [WallpaperPointButton setTag:21];
    [wallPaperView addSubview:WallpaperPointButton];
    
    UIButton* WallpaperCeilButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [WallpaperCeilButton setFrame:CGRectMake(160, 40, 60, 50)];
    [WallpaperCeilButton setTitle:[NSString stringWithFormat:@"천장"] forState:UIControlStateNormal];
    [WallpaperCeilButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [WallpaperCeilButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [WallpaperCeilButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [WallpaperCeilButton addTarget:self action:@selector(WallpaperUseButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [WallpaperCeilButton setTag:22];
    [wallPaperView addSubview:WallpaperCeilButton];
    
    UIButton* WallpaperFullWidthButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [WallpaperFullWidthButton setFrame:CGRectMake(230, 40, 60, 50)];
    [WallpaperFullWidthButton setTitle:[NSString stringWithFormat:@"전폭"] forState:UIControlStateNormal];
    [WallpaperFullWidthButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [WallpaperFullWidthButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [WallpaperFullWidthButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [WallpaperFullWidthButton addTarget:self action:@selector(WallpaperUseButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [WallpaperFullWidthButton setTag:23];
    [wallPaperView addSubview:WallpaperFullWidthButton];
    
    UIButton* WallpaperBandButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [WallpaperBandButton setFrame:CGRectMake(300, 40, 60, 50)];
    [WallpaperBandButton setTitle:[NSString stringWithFormat:@"띠벽지"] forState:UIControlStateNormal];
    [WallpaperBandButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [WallpaperBandButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [WallpaperBandButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [WallpaperBandButton addTarget:self action:@selector(WallpaperUseButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [WallpaperBandButton setTag:24];
    [wallPaperView addSubview:WallpaperBandButton];
    
    UIButton* AllColorButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [AllColorButton setFrame:CGRectMake(10, 140, 50, 50)];
    [AllColorButton setTitle:[NSString stringWithFormat:@"전체"] forState:UIControlStateNormal];
    [AllColorButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [AllColorButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [AllColorButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [AllColorButton addTarget:self action:@selector(ColorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [AllColorButton setTag:30];
    [wallPaperView addSubview:AllColorButton];
    
    int buttonWidth = 50/2;
    int buttonHeight = 50/2;
    int buttonMarignRight = 5;
    int buttonMarginBottom = 5;
    int rowCount =  9;
    int buttonX = 0;
    int buttonY = 0;
    for(int i = 0; i < 18; i++)
    {
       UIButton* ColorButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        buttonX = (i % rowCount) * (buttonWidth + buttonMarignRight) + 80;
        buttonY = (buttonHeight + buttonMarginBottom) * (i / rowCount) + 137;
       [ColorButton setFrame:CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight)];
       [ColorButton setTag:31+i];
       [ColorButton addTarget:self action:@selector(ColorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
       [wallPaperView addSubview:ColorButton];
    }
    
    self.NameTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 220, 200, 30)];
    [self.NameTextField setBackgroundColor:[UIColor whiteColor]];
    [self.NameTextField setDelegate:self];
    [self.NameTextField setBackgroundColor:[UIColor colorWithRed:0.94f green:0.94f blue:0.94f alpha:1.0f]];
    [self.NameTextField setTextColor:[UIColor colorWithRed:0.42f green:0.42f blue:0.42f alpha:1.0f]];
    [self.NameTextField setBorderStyle:UITextBorderStyleLine];
    [self.NameTextField setFont:[UIFont systemFontOfSize:20]];
    [wallPaperView addSubview:self.NameTextField];
    
    self.PhoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 270, 200, 30)];
    [self.PhoneTextField setBackgroundColor:[UIColor whiteColor]];
    [self.PhoneTextField setDelegate:self];
    [self.PhoneTextField setBackgroundColor:[UIColor colorWithRed:0.94f green:0.94f blue:0.94f alpha:1.0f]];
    [self.PhoneTextField setTextColor:[UIColor colorWithRed:0.42f green:0.42f blue:0.42f alpha:1.0f]];
    [self.PhoneTextField setBorderStyle:UITextBorderStyleLine];
    [self.PhoneTextField setFont:[UIFont systemFontOfSize:20]];
    [wallPaperView addSubview:self.PhoneTextField];
    
    self.EmailTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 320, 200, 30)];
    [self.EmailTextField setBackgroundColor:[UIColor whiteColor]];
    [self.EmailTextField setDelegate:self];
    [self.EmailTextField setBackgroundColor:[UIColor colorWithRed:0.94f green:0.94f blue:0.94f alpha:1.0f]];
    [self.EmailTextField setTextColor:[UIColor colorWithRed:0.42f green:0.42f blue:0.42f alpha:1.0f]];
    [self.EmailTextField setBorderStyle:UITextBorderStyleLine];
    [self.EmailTextField setFont:[UIFont systemFontOfSize:20]];
    [wallPaperView addSubview:self.EmailTextField];
    
    self.MemoTextField = [[UITextView alloc] initWithFrame:CGRectMake(150, 370, 200, 130)];
    [self.MemoTextField setBackgroundColor:[UIColor whiteColor]];
//    [self.MemoTextField setDelegate:self];
    [self.MemoTextField setBackgroundColor:[UIColor colorWithRed:0.94f green:0.94f blue:0.94f alpha:1.0f]];
    [self.MemoTextField setTextColor:[UIColor colorWithRed:0.42f green:0.42f blue:0.42f alpha:1.0f]];
//    [self.MemoTextField setBorderStyle:UITextBorderStyleLine];
    [self.MemoTextField setFont:[UIFont systemFontOfSize:20]];
    
    [wallPaperView addSubview:self.MemoTextField];

   
    [leftMenuScrollView addSubview:wallPaperView];
    
}

-(void)PriceButtonPressed:(id)sender
{
    
}

-(void)WallpaperOrFlooringButtonPressed:(id)sender
{
    
}
-(void)WallpaperUseButtonPressed:(id)sender
{
    
}
-(void)ColorButtonPressed:(id)sender
{
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

@end
